using System.Collections;
using UnityEngine;

public class BallState_Pass : AbstractBallState
{
	public static BallState_Pass instance;

	private BallState_Pass()
	{
	}

	public static BallState_Pass getInstance()
	{
		if (instance == null)
		{
			instance = new BallState_Pass();
		}
		return instance;
	}

	public override void enter(Ball owner)
	{
		base.enter(owner);
		if (owner.isRerferPass)
		{
			owner.isInSomeOneHand = true;
		}
		else
		{
			owner.isInSomeOneHand = true;
			owner.pass_playerType_failto_garb_steal = new Hashtable();
			owner.setVelocityZero();
		}
		owner.isCatchedPass = false;
		owner.isStealGrab = false;
		owner.isWillStealGrab = DSS.IsStealSuccess(owner.pass_player_catch_ball.paraPlayer, owner.pass_player_catch_ball.paraPlayer.getData_Steal(), isGrabSteal: true);
		if ((owner.pass_player_catch_ball.teamType == TeamType.COMPUTER && UIChooseTeam.comLevel >= 6) || (owner.pass_player_catch_ball.teamType == TeamType.USER && GameData.TeamLevel[UIChooseTeam.indexInTeamData_User] >= 6))
		{
			if (MathHelper.isInPercent(50f))
			{
				owner.pass_player_catch_ball.catch_is_normal_catch = true;
			}
			else
			{
				owner.pass_player_catch_ball.catch_is_normal_catch = false;
			}
		}
		else
		{
			owner.pass_player_catch_ball.catch_is_normal_catch = true;
		}
		if (!owner.isRerferPass)
		{
			if (owner.pass_player_catch_ball.teamType == TeamType.USER)
			{
				Instance<Court>.instance.count_pauseData_PassTotal_User++;
			}
			else
			{
				Instance<Court>.instance.count_pauseData_PassTotal_Com++;
			}
		}
		CameraController.timeMoveCamera = 0.5f;
	}

	public override void fixedUpdate(Ball owner)
	{
		base.fixedUpdate(owner);
		if (owner.isRerferPass)
		{
			if (!owner.isInSomeOneHand)
			{
				owner.pass_player_catch_ball.catch_is_normal_catch = true;
				GameHelper.MoveTo(owner.myTransform, MathHelper.setY(owner.pass_player_catch_ball.getPosition(), 0.8f), Global.speed_ball_pass);
				if (MathHelper.getSqrDistance(MathHelper.setY(owner.getPosition(), 0f), owner.pass_player_catch_ball.getPosition()) <= 4f)
				{
					owner.pass_player_catch_ball.OnCatchBall();
				}
			}
		}
		else if (owner.isPassSuccess)
		{
			if (owner.isInSomeOneHand)
			{
				return;
			}
			GameHelper.MoveTo(owner.myTransform, MathHelper.setY(owner.pass_player_catch_ball.getPosition(), 0.8f), Global.speed_ball_pass);
			if (!(MathHelper.getSqrDistance(MathHelper.setY(owner.getPosition(), 0f), owner.pass_player_catch_ball.getPosition()) <= 4f))
			{
				return;
			}
			owner.pass_player_catch_ball.OnCatchBall();
			if (!owner.isCatchedPass)
			{
				owner.isCatchedPass = true;
				if (owner.pass_player_catch_ball.teamType == TeamType.USER)
				{
					Instance<Court>.instance.count_PauseData_User[4]++;
				}
				else
				{
					Instance<Court>.instance.count_PauseData_Com[4]++;
				}
			}
		}
		else if (!owner.isInSomeOneHand)
		{
			GameHelper.MoveTo(owner.myTransform, owner.getPosition() + owner.pass_wrong_direction, Global.speed_ball_pass);
			owner.player_make_ball_outside = owner.pass_player_catch_ball;
			owner.IsBallOutSide();
		}
	}

	private float getToPassLineSqrDistance(Player paraPlayer, Player catchBallPlayer, Ball owner)
	{
		Vector3 position = catchBallPlayer.getPosition();
		float z = position.z;
		Vector3 position2 = owner.getPosition();
		float num = z - position2.z;
		Vector3 position3 = paraPlayer.getPosition();
		float num2 = num * position3.x;
		Vector3 position4 = owner.getPosition();
		float x = position4.x;
		Vector3 position5 = catchBallPlayer.getPosition();
		float num3 = x - position5.x;
		Vector3 position6 = paraPlayer.getPosition();
		float num4 = num2 + num3 * position6.z;
		Vector3 position7 = catchBallPlayer.getPosition();
		float x2 = position7.x;
		Vector3 position8 = owner.getPosition();
		float num5 = x2 * position8.z;
		Vector3 position9 = catchBallPlayer.getPosition();
		float z2 = position9.z;
		Vector3 position10 = owner.getPosition();
		float num6 = Mathf.Pow(num4 + (num5 - z2 * position10.x), 2f);
		Vector3 position11 = catchBallPlayer.getPosition();
		float z3 = position11.z;
		Vector3 position12 = owner.getPosition();
		float num7 = Mathf.Pow(z3 - position12.z, 2f);
		Vector3 position13 = owner.getPosition();
		float x3 = position13.x;
		Vector3 position14 = catchBallPlayer.getPosition();
		return num6 / (num7 + Mathf.Pow(x3 - position14.x, 2f));
	}

	public override void exit(Ball owner)
	{
		base.exit(owner);
		CameraController.timeMoveCamera = 0.8f;
	}
}
